package com.example.zkweixiu.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.myview.PhoneCode;
import com.example.zkweixiu.app.pro.PublicPro;
import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.yanzhenjie.sofia.Sofia;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

// 密码修改 - 验证手机号
public class ChangePassword3Activity extends AppCompatActivity implements View.OnClickListener {
    private Intent intent;
    private TextView tv_phone;
    private TextView workbox; //工具箱
    private RelativeLayout rl_change;
    private TextView tv_again;
    private PhoneCode pc_1;
    private String guaPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password3);
        Sofia.with(ChangePassword3Activity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));

        tv_phone = findViewById(R.id.tv_phone); //手机号
        tv_again = findViewById(R.id.tv_again);
        rl_change  = findViewById(R.id.rl_change);
        workbox = findViewById(R.id.workbox); //工具箱

        //验证码
        pc_1 = findViewById(R.id.pc_1);

        ImageView iv_fanhui = findViewById(R.id.iv_fanhui);
        View iv_fanhui2 = findViewById(R.id.iv_fanhui2);

        iv_fanhui.setOnClickListener(this);
        iv_fanhui2.setOnClickListener(this);
        workbox.setOnClickListener(this); //工具箱

        rl_change.setOnClickListener(this); //下一步
        tv_again.setOnClickListener(this); //重新发送验证码

        tv_again.setClickable(false);
        if (PublicPro.userBean != null) {

            guaPhone = PublicPro.userBean.getData().getGuaPhone();
            StringBuilder sb = new StringBuilder(guaPhone);
            sb.replace(3,9,"******");
            tv_phone.setText(sb.toString());

            sendSIM(guaPhone); //
        }

        RxActivityTool.addActivity(ChangePassword3Activity.this);


    }
    @Override
    public void onResume() {
        super.onResume();

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_change:
                String phoneCode = pc_1.getPhoneCode();

                if (TextUtils.isEmpty(phoneCode)){
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }

                intent = new Intent(this, ChangePassword21Activity.class);//  密码修改 - 验证手机号 - 修改密码
                intent.putExtra("code",phoneCode);
                startActivity(intent);
                break;
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.iv_fanhui2:
                finish();
                break;
            case R.id.tv_again:   //重新发送验证码

                if (TextUtils.isEmpty(guaPhone)){
                    Toast.makeText(this, "手机号码为空", Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                }

                sendSIM(guaPhone);
                break;
            case R.id.workbox:   //工具箱
    /*            intent = new Intent(this, ChangePassword31Activity.class);
                startActivity(intent);*/
                break;
        }
    }


    /**
     * 发送验证码
     * */
    private void sendSIM(final String phone2) {

        OkHttpUtils
                .get()
                .url(PublicPro.PUT_SIM)
                .addParams("phone", phone2)
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                        rl_change.setEnabled(true);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);


                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);

                        if (objBean.getStatus() == 200) {
                            Toast.makeText(ChangePassword3Activity.this, "发送成功", Toast.LENGTH_SHORT).show();

                            CountDownTimer timer = new CountDownTimer(60000, 10) {
                                public void onTick(long millisUntilFinished) {
                                    tv_again.setClickable(false);
                                    tv_again.setText(millisUntilFinished / 1000 + "秒");
                                }

                                public void onFinish() {
                                    tv_again.setText("重新发送");
                                    tv_again.setClickable(true);
                                }
                            };

                            timer.start();
                        } else {
                            rl_change.setEnabled(true);
                            Toast.makeText(ChangePassword3Activity.this, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }
}
